const cache = (storage) => ({
  set(key, value) {
    if (!storage || key == null || value == null) return;
    storage.setItem(key, value);
  },
  get(key) {
    if (!storage || key == null) return null;
    return storage.getItem(key);
  },
  setJSON(key, jsonValue) {
    if (jsonValue == null) return;
    this.set(key, JSON.stringify(jsonValue));
  },
  getJSON(key) {
    const value = this.get(key);
    return value ? JSON.parse(value) : null;
  },
  remove(key) {
    if (!storage || key == null) return;
    storage.removeItem(key);
  },
  clear() {
    if (!storage) return;
    storage.clear();
  }
});

export default {
  session: cache(sessionStorage),
  local: cache(localStorage)
};